package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.CategoryPageJoinModal;
import com.opensky.qatest.ui.component.CategoryPageJoinNag;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * REFACTORED
 * 
 * OpenSky - "Category page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class CategoryPage extends LoadableComponent<CategoryPage> implements IPage {

    // ************************ Locators ************************

    private static final String HELP_LINK = "//div[@id='sky-bar']//a[@href='/content/faq']";

    private static final String LOGIN_LINK = "//div[@id='sky-bar']//div[@class='login']/a";

    private static final String TOP_NAV = "//ul[@id='category-nav']";

    private static final String BOTTOM_NAV = "//div[@class='wrap']/ul[@class='links']";

    private static final String FOLLOW_CURATOR_BTN = "//div[@id='curator-sales']//li[@class='thin'][1]//a[@class='button follow-button']";

    private static final String TOOL_TIP = "//div[@aria-hidden='false']/div[@class='ui-tooltip-content']";

    private static final String TOOL_TIP_LINK = "//div[@aria-hidden='false']/div[@class='ui-tooltip-content']//a";

    private static final String CURATOR_IMG = "//div[@id='curator-sales']//li[@class='thin'][1]//div[@class='curator']//img";

    private static final String CURATOR_IMG_LINK = "//div[@id='curator-sales']//li[@class='thin'][1]//div[@class='curator']//a";

    private String categoryPage = Properties.get("env.root.url") + "curators/%s";

    // ************************ Complex page objects **************************

    private CategoryPageJoinModal joinModal;

    private CategoryPageJoinNag joinNag;

    private final WebDriverWrapper driver;

    public CategoryPage(WebDriverWrapper driver, String category) {
	this.driver = driver;
	joinModal = new CategoryPageJoinModal(driver);
	joinNag = new CategoryPageJoinNag(driver);
	categoryPage = String.format(categoryPage, category.toLowerCase());
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(categoryPage);

    }

    public void clickFollowCuratorBtn() {
	driver.waitAndClick("FOLLOW_CURATOR_BTN", By.xpath(FOLLOW_CURATOR_BTN));
    }

    public CuratorsProfilePage clickCuratorImgAndCheckURL() {

	driver.waitAndVerifyElementPresent("CURATOR_IMG", By.xpath(CURATOR_IMG));
	WebElement link = driver.waitAssertAndGetElement("CURATOR_IMG_LINK", By.xpath(CURATOR_IMG_LINK));

	String href = link.getAttribute(HTML.HREF);
	String curator = href.split("/")[3];

	CuratorsProfilePage curatorsFrofilePage = new CuratorsProfilePage(driver, curator);

	driver.waitAndSendEnter("CURATOR_IMG_LINK", By.xpath(CURATOR_IMG_LINK));

	if (curatorsFrofilePage.isPageLoaded()) {
	    return curatorsFrofilePage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(categoryPage, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(categoryPage);
    }

    public boolean isBaseNavElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK)),
		driver.waitAndVerifyElementPresent("HELP_LINK", By.xpath(HELP_LINK)),
		driver.waitAndVerifyElementPresent("TOP_NAV", By.xpath(TOP_NAV)),
		driver.waitAndVerifyElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV)));
    }

    public boolean isToolTipPresent() {

	return driver.waitAndVerifyElementPresent("TOOL_TIP", By.xpath(TOOL_TIP));
    }

    public LoginPage clickToolTipLogIn() {

	LoginPage loginPage = new LoginPage(driver);

	driver.waitAndClick("TOOL_TIP_LINK", By.xpath(TOOL_TIP_LINK));

	if (loginPage.isPageLoaded()) {
	    return loginPage;
	} else {
	    return null;
	}
    }

    // ************************ GET/SET methods **************************

    public CategoryPageJoinModal getJoinModal() {
	return joinModal.isComponentLoaded() ? joinModal : null;
    }

    public CategoryPageJoinNag getJoinNag() {
	return joinNag.isComponentLoaded() ? joinNag : null;
    }

    @Override
    public String getName() {
	return "Category page";
    }
}
